home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 404_02 / bisnp / bison.hairy < prev    next >
Encoding:
Text File  |  1993-02-24  |  6.4 KB  |  342 lines

  1. /* header section */
  2. #include <stdio.h>
  3. #ifndef __STDC__
  4. #define const
  5. #endif
  6.  
  7.  
  8. $
  9. extern int timeclock;
  10.  
  11.  
  12. int yyerror;        /*  Yyerror and yycost are set by guards.    */
  13. int yycost;        /*  If yyerror is set to a nonzero value by a    */
  14.             /*  guard, the reduction with which the guard    */
  15.             /*  is associated is not performed, and the    */
  16.             /*  error recovery mechanism is invoked.    */
  17.             /*  Yycost indicates the cost of performing    */
  18.             /*  the reduction given the attributes of the    */
  19.             /*  symbols.                    */
  20.  
  21.  
  22. /*  YYMAXDEPTH indicates the size of the parser's state and value    */
  23. /*  stacks.                                */
  24.  
  25. #ifndef    YYMAXDEPTH
  26. #define    YYMAXDEPTH    500
  27. #endif
  28.  
  29. /*  YYMAXRULES must be at least as large as the number of rules that    */
  30. /*  could be placed in the rule queue.  That number could be determined    */
  31. /*  from the grammar and the size of the stack, but, as yet, it is not.    */
  32.  
  33. #ifndef    YYMAXRULES
  34. #define    YYMAXRULES    100
  35. #endif
  36.  
  37. #ifndef    YYMAXBACKUP
  38. #define YYMAXBACKUP    100
  39. #endif
  40.  
  41.  
  42. short    yyss[YYMAXDEPTH];    /*  the state stack            */
  43. YYSTYPE    yyvs[YYMAXDEPTH];    /*  the semantic value stack        */
  44. YYLTYPE yyls[YYMAXDEPTH];    /*  the location stack            */
  45. short    yyrq[YYMAXRULES];    /*  the rule queue            */
  46. int    yychar;            /*  the lookahead symbol        */
  47.  
  48. YYSTYPE    yylval;            /*  the semantic value of the        */
  49.                 /*  lookahead symbol            */
  50.  
  51. YYSTYPE yytval;            /*  the semantic value for the state    */
  52.                 /*  at the top of the state stack.    */
  53.  
  54. YYSTYPE yyval;            /*  the variable used to return        */
  55.                 /*  semantic values from the action    */
  56.                 /*  routines                */
  57.  
  58. YYLTYPE yylloc;        /*  location data for the lookahead    */
  59.                 /*  symbol                */
  60.  
  61. YYLTYPE yytloc;        /*  location data for the state at the    */
  62.                 /*  top of the state stack        */
  63.  
  64.  
  65. int    yynunlexed;
  66. short    yyunchar[YYMAXBACKUP];
  67. YYSTYPE    yyunval[YYMAXBACKUP];
  68. YYLTYPE yyunloc[YYMAXBACKUP];
  69.  
  70. short *yygssp;            /*  a pointer to the top of the state    */
  71.                 /*  stack; only set during error    */
  72.                 /*  recovery.                */
  73.  
  74. YYSTYPE *yygvsp;        /*  a pointer to the top of the value    */
  75.                 /*  stack; only set during error    */
  76.                 /*  recovery.                */
  77.  
  78. YYLTYPE *yyglsp;        /*  a pointer to the top of the        */
  79.                 /*  location stack; only set during    */
  80.                 /*  error recovery.            */
  81.  
  82.  
  83. /*  Yyget is an interface between the parser and the lexical analyzer.    */
  84. /*  It is costly to provide such an interface, but it avoids requiring    */
  85. /*  the lexical analyzer to be able to back up the scan.        */
  86.  
  87. yyget()
  88. {
  89.   if (yynunlexed > 0)
  90.     {
  91.       yynunlexed--;
  92.       yychar = yyunchar[yynunlexed];
  93.       yylval = yyunval[yynunlexed];
  94.       yylloc = yyunloc[yynunlexed];
  95.     }
  96.   else if (yychar <= 0)
  97.     yychar = 0;
  98.   else
  99.     {
  100.       yychar = yylex();
  101.       if (yychar < 0)
  102.     yychar = 0;
  103.       else yychar = YYTRANSLATE(yychar);
  104.     }
  105. }
  106.  
  107.  
  108.  
  109. yyunlex(chr, val, loc)
  110. int chr;
  111. YYSTYPE val;
  112. YYLTYPE loc;
  113. {
  114.   yyunchar[yynunlexed] = chr;
  115.   yyunval[yynunlexed] = val;
  116.   yyunloc[yynunlexed] = loc;
  117.   yynunlexed++;
  118. }
  119.  
  120.  
  121.  
  122. yyrestore(first, last)
  123. register short *first;
  124. register short *last;
  125. {
  126.   register short *ssp;
  127.   register short *rp;
  128.   register int symbol;
  129.   register int state;
  130.   register int tvalsaved;
  131.  
  132.   ssp = yygssp;
  133.   yyunlex(yychar, yylval, yylloc);
  134.  
  135.   tvalsaved = 0;
  136.   while (first != last)
  137.     {
  138.       symbol = yystos[*ssp];
  139.       if (symbol < YYNTBASE)
  140.     {
  141.       yyunlex(symbol, yytval, yytloc);
  142.       tvalsaved = 1;
  143.       ssp--;
  144.     }
  145.  
  146.       ssp--;
  147.  
  148.       if (first == yyrq)
  149.     first = yyrq + YYMAXRULES;
  150.  
  151.       first--;
  152.  
  153.       for (rp = yyrhs + yyprhs[*first]; symbol = *rp; rp++)
  154.     {
  155.       if (symbol < YYNTBASE)
  156.         state = yytable[yypact[*ssp] + symbol];
  157.       else
  158.         {
  159.           state = yypgoto[symbol - YYNTBASE] + *ssp;
  160.  
  161.           if (state >= 0 && state <= YYLAST && yycheck[state] == *ssp)
  162.         state = yytable[state];
  163.           else
  164.         state = yydefgoto[symbol - YYNTBASE];
  165.         }
  166.  
  167.       *++ssp = state;
  168.     }
  169.     }
  170.  
  171.   if ( ! tvalsaved && ssp > yyss)
  172.     {
  173.       yyunlex(yystos[*ssp], yytval, yytloc);
  174.       ssp--;
  175.     }
  176.  
  177.   yygssp = ssp;
  178. }
  179.  
  180.  
  181.  
  182. int
  183. yyparse()
  184. {
  185.   register int yystate;
  186.   register int yyn;
  187.   register short *yyssp;
  188.   register short *yyrq0;
  189.   register short *yyptr;
  190.   register YYSTYPE *yyvsp;
  191.  
  192.   int yylen;
  193.   YYLTYPE *yylsp;
  194.   short *yyrq1;
  195.   short *yyrq2;
  196.  
  197.   yystate = 0;
  198.   yyssp = yyss - 1;
  199.   yyvsp = yyvs - 1;
  200.   yylsp = yyls - 1;
  201.   yyrq0 = yyrq;
  202.   yyrq1 = yyrq0;
  203.   yyrq2 = yyrq0;
  204.  
  205.   yychar = yylex();
  206.   if (yychar < 0)
  207.     yychar = 0;
  208.   else yychar = YYTRANSLATE(yychar);
  209.  
  210. yynewstate:
  211.  
  212.   if (yyssp >= yyss + YYMAXDEPTH - 1)
  213.     {
  214.       yyabort("Parser Stack Overflow");
  215.       YYABORT;
  216.     }
  217.  
  218.   *++yyssp = yystate;
  219.  
  220. yyresume:
  221.  
  222.   yyn = yypact[yystate];
  223.   if (yyn == YYFLAG)
  224.     goto yydefault;
  225.  
  226.   yyn += yychar;
  227.   if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar)
  228.     goto yydefault;
  229.  
  230.   yyn = yytable[yyn];
  231.   if (yyn < 0)
  232.     {
  233.       yyn = -yyn;
  234.       goto yyreduce;
  235.     }
  236.   else if (yyn == 0)
  237.     goto yyerrlab;
  238.  
  239.   yystate = yyn;
  240.  
  241.   yyptr = yyrq2;
  242.   while (yyptr != yyrq1)
  243.     {
  244.       yyn = *yyptr++;
  245.       yylen = yyr2[yyn];
  246.       yyvsp -= yylen;
  247.       yylsp -= yylen;
  248.  
  249.       yyguard(yyn, yyvsp, yylsp);
  250.       if (yyerror)
  251.     goto yysemerr;
  252.  
  253.       yyaction(yyn, yyvsp, yylsp);
  254.       *++yyvsp = yyval;
  255.  
  256.       yylsp++;
  257.       if (yylen == 0)
  258.     {
  259.       yylsp->timestamp = timeclock;
  260.       yylsp->first_line = yytloc.first_line;
  261.       yylsp->first_column = yytloc.first_column;
  262.       yylsp->last_line = (yylsp-1)->last_line;
  263.       yylsp->last_column = (yylsp-1)->last_column;
  264.       yylsp->text = 0;
  265.     }
  266.       else
  267.     {
  268.       yylsp->last_line = (yylsp+yylen-1)->last_line;
  269.       yylsp->last_column = (yylsp+yylen-1)->last_column;
  270.     }
  271.       
  272.       if (yyptr == yyrq + YYMAXRULES)
  273.         yyptr = yyrq;
  274.     }
  275.  
  276.   if (yystate == YYFINAL)
  277.     YYACCEPT;
  278.  
  279.   yyrq2 = yyptr;
  280.   yyrq1 = yyrq0;
  281.  
  282.   *++yyvsp = yytval;
  283.   *++yylsp = yytloc;
  284.   yytval = yylval;
  285.   yytloc = yylloc;
  286.   yyget();
  287.  
  288.   goto yynewstate;
  289.  
  290. yydefault:
  291.  
  292.   yyn = yydefact[yystate];
  293.   if (yyn == 0)
  294.     goto yyerrlab;
  295.  
  296. yyreduce:
  297.  
  298.   *yyrq0++ = yyn;
  299.  
  300.   if (yyrq0 == yyrq + YYMAXRULES)
  301.     yyrq0 = yyrq;
  302.  
  303.   if (yyrq0 == yyrq2)
  304.     {
  305.       yyabort("Parser Rule Queue Overflow");
  306.       YYABORT;
  307.     }
  308.  
  309.   yyssp -= yyr2[yyn];
  310.   yyn = yyr1[yyn];
  311.  
  312.   yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
  313.   if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
  314.     yystate = yytable[yystate];
  315.   else
  316.     yystate = yydefgoto[yyn - YYNTBASE];
  317.  
  318.   goto yynewstate;
  319.  
  320. yysemerr:
  321.   *--yyptr = yyn;
  322.   yyrq2 = yyptr;
  323.   yyvsp += yyr2[yyn];
  324.  
  325. yyerrlab:
  326.  
  327.   yygssp = yyssp;
  328.   yygvsp = yyvsp;
  329.   yyglsp = yylsp;
  330.   yyrestore(yyrq0, yyrq2);
  331.   yyrecover();
  332.   yystate = *yygssp;
  333.   yyssp = yygssp;
  334.   yyvsp = yygvsp;
  335.   yyrq0 = yyrq;
  336.   yyrq1 = yyrq0;
  337.   yyrq2 = yyrq0;
  338.   goto yyresume;
  339. }
  340.  
  341. $
  342.